import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class HttputilsService {

  constructor(public http: HttpClient) { }
  // get请求
  get(url: string) {
    return new Promise((resolve, reject) => {
      this.http.get(url).subscribe(resolve);
    });
  }
  //  post请求
  post(url: string, data: any) {
    const httpOptions = {
      headers: new HttpHeaders({'Content-Type': 'application/json'})
    }
    return new Promise((resolve, reject) => {
      this.http.post(url, JSON.stringify(data), httpOptions).subscribe(resolve);
    });
  }
  // jsonp请求
  jsonp(url: string) {
    return new Promise((resolve, reject) => {
      this.http.jsonp(url, 'callback').subscribe(resolve);
    });
  }
}
